import java.awt.Image
import javax.swing.ImageIcon
import java.awt.image.BufferedImage
import java.awt.Graphics2D
import javax.imageio.ImageIO

import java.awt.RenderingHints
import java.io.*
import java.awt.image.renderable.*
import javax.media.jai.*
import com.sun.media.jai.codec.*


/**
 * Picture class
 */
public class Picture{
   private static final float thumbnailSize = 100
   private static final String IMAGE_URI = "/PictureCentral/images/userImages/"
   private static final String IMAGE_LOCATION = "web-app/images/userImages/"
   private static final String JPEG = ".jpg"
   private static final String THUMBNAIL = "_thumbnail"
   

   /* Removes the accelaration lib exception */
   static { System.setProperty("com.sun.media.jai.disableMediaLib", "true"); } 
   
   String imagePath
   String thumbnailPath
   String name
   String description
   String category
   Double price   
   Date createdOn = new Date()   
   AuthenticationUser user
   
   static hasMany = [orders:UserOrder]
   static belongsTo = [UserOrder]
   
   static fetchMode = [user:'eager']
   static mapping = {
          columns {
        	  user lazy:true
          }
   }

	/**
	 * This method saves the image.
	 */
	public void setImagePath(byte[] imageData, String fileName) {
	    ByteArraySeekableStream byteStream = new ByteArraySeekableStream(imageData);
		RenderedOp imageStream = JAI.create("stream", byteStream);
		String imageName = fileName+JPEG
		File imageFile = new File(IMAGE_LOCATION+imageName);
	    imageFile.createNewFile();
	    setImagePath(IMAGE_URI+imageName);
		FileOutputStream outputStream = new FileOutputStream(imageFile.getPath());
		JAI.create("encode", imageStream, outputStream, "JPEG", null);
		outputStream.close();
		setThumbnailPath(imageData, fileName);
	}
   
   public void setImagePath(String path) {
	   imagePath = path;	   
   }
   
   /**
	 * This method creates and saves a thumbnail.
	 */
	private void setThumbnailPath(byte[] imageData, String fileName) {
		ByteArraySeekableStream byteStream = new ByteArraySeekableStream(imageData);
		RenderedOp imageStream = JAI.create("stream", byteStream);
		int width = imageStream.getWidth();
		int height = imageStream.getHeight();
		
		boolean tall = (height > width);
		float modifier = thumbnailSize / (float) (tall ? height : width);
		
		ParameterBlock params = new ParameterBlock();
		params.addSource(imageStream);
		params.add(modifier);//x scale factor
		params.add(modifier);//y scale factor
		params.add(0.0F);//x translate
		params.add(0.0F);//y translate
		params.add(new InterpolationNearest());//interpolation method
		RenderedOp resizedImage = JAI.create("scale", params);
		
		String imageName = fileName+THUMBNAIL+JPEG;
		File imageFile = new File(IMAGE_LOCATION+imageName);
	    imageFile.createNewFile();
	    setThumbnailPath(IMAGE_URI+imageName);
		FileOutputStream outputStream = new FileOutputStream(imageFile.getPath());
		JAI.create("encode", resizedImage, outputStream, "JPEG", null);
		outputStream.close();
	}
   
    public void setThumbnailPath(String path) {
    	thumbnailPath = path;
    }
    
    public String getOnlyFilename(String path) {
    	return MyLeftStr(MyRightStr(path,IMAGE_URI),JPEG);
    }

    public String getFilenameAndExtension(String path) {
    	return MyRightStr(path,IMAGE_URI);
    }

    public String getLongPath(String path) {
    	return IMAGE_LOCATION + getFilenameAndExtension(path);
    }
    
    // MyRightStr and MyLeftStr from http://www.mindwatering.net/
    public String MyRightStr(String bigstr, String smstr) {
    	int strlen = bigstr.length();
    	int strpos = bigstr.indexOf(smstr);
    	if (strpos == -1) {
    		// not found - return nothing
    		return "";
    	} else {
    		String resultstr = bigstr.substring(strpos + smstr.length(), strlen);
    		return resultstr.trim();
    	}
    }
    
    public String MyLeftStr(String bigstr, String smstr) {
    	int strpos = bigstr.indexOf(smstr);
    	if (strpos == -1) {
    		// not found - return nothing
    		return "";
    	} else {
    		String resultstr = bigstr.substring(0, strpos);
    		return resultstr;
    	}
    }
    
    public String getCategory(Locale locale) {
    	
    }
}
